package com.example.taskschedule.application.task;

import com.example.taskschedule.domain.task.MyTask;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 任务提交器，编排任务的执行，属于应用层
 * 每秒提交指定数量的任务到线程池
 */
public class TaskSubmitter implements Runnable{
    private final ThreadPoolExecutor threadPool;
    private final int taskPerSecond;
    private final AtomicInteger TASK_ID_GENERATOR = new AtomicInteger(1);
    public TaskSubmitter(ThreadPoolExecutor threadPool, int taskPerSecond) {
        this.threadPool = threadPool;
        this.taskPerSecond = taskPerSecond;
    }
    @Override
    public void run() {
        for(int i = 0; i < taskPerSecond; i++){
            int taskId=TASK_ID_GENERATOR.getAndIncrement();
            threadPool.execute(new MyTask(taskId));
        }
    }
}
